package org.dreamwork.seagragon;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2010-4-24
 * Time: 18:07:31
 */
public class ImitationHttpRequest implements HttpServletRequest {
    private String url;
    private Map<String, Object> params = new HashMap<String, Object> ();
    private Map<String, Object> attributes = new HashMap<String, Object> ();
    private String queryString, servletPath;
    private ServletContext context;

    @SuppressWarnings ("unchecked")
    public ImitationHttpRequest (ServletContext context, String url) {
        this.context = context;
        this.url = url;
        this.servletPath = url;
        int pos = url.indexOf ('?');
        if (pos > -1) {
            queryString = url.substring (pos + 1);
            servletPath = url.substring (0, pos);
        }
        if (queryString != null) {
            String[] a = queryString.split ("&");
            for (String p : a) {
                String[] t = p.split ("=");
                Object old = params.get (t [0]);
                if (old == null) params.put (t[0], t[1]);
                else if (old instanceof String) {
                    List<String> list = new ArrayList<String> ();
                    list.add ((String) old);
                    list.add (t[1]);
                    params.put (t[0], list);
                } else if (old instanceof List) {
                    ((List<String>) old).add (t[1]);
                }
            }
        }
    }

    public String getAuthType () {
        return null;
    }

    public Cookie[] getCookies () {
        return null;
    }

    public long getDateHeader (String s) {
        
        return 0;
    }

    public String getHeader (String s) {
        return null;
    }

    public Enumeration getHeaders (String s) {
        return null;
    }

    public Enumeration getHeaderNames () {
        return null;
    }

    public int getIntHeader (String s) {
        return 0;
    }

    public String getMethod () {
        return "GET";
    }

    public String getPathInfo () {
        return servletPath;
    }

    public String getPathTranslated () {
        return null;
    }

    public String getContextPath () {
        String contextName = context.getServletContextName ();
        if (contextName == null || contextName.trim ().length () == 0) return "";
        contextName = contextName.trim ();
        if (contextName.charAt (0) != '/') contextName = "/" + contextName;
        return contextName;
    }

    public String getQueryString () {
        return queryString;
    }

    public String getRemoteUser () {
        return null;
    }

    public boolean isUserInRole (String s) {
        return false;
    }

    public Principal getUserPrincipal () {
        return null;
    }

    public String getRequestedSessionId () {
        return null;
    }

    public String getRequestURI () {
        return url;
    }

    public StringBuffer getRequestURL () {
        return new StringBuffer (url);
    }

    public String getServletPath () {
        return servletPath;
    }

    public HttpSession getSession (boolean b) {
        return null;
    }

    public HttpSession getSession () {
        return null;
    }

    @Deprecated
    public boolean isRequestedSessionIdValid () {
        return false;
    }

    public boolean isRequestedSessionIdFromCookie () {
        return false;
    }

    public boolean isRequestedSessionIdFromURL () {
        return false;
    }

    @Deprecated
    public boolean isRequestedSessionIdFromUrl () {
        return false;
    }

    public Object getAttribute (String s) {
        return attributes.get (s);
    }

    public Enumeration getAttributeNames () {
        return new ReadOnlyEnumeration (attributes.keySet ());
    }

    public String getCharacterEncoding () {
        return "utf-8";
    }

    public void setCharacterEncoding (String s) throws UnsupportedEncodingException {

    }

    public int getContentLength () {
        return 0;
    }

    public String getContentType () {
        return null;
    }

    public ServletInputStream getInputStream () throws IOException {
        return null;
    }

    @SuppressWarnings ("unchecked")
    public String getParameter (String s) {
        Object old = params.get (s);
        if (old == null) return null;
        if (old instanceof String) return (String) old;
        else {
            List<String> list = (List<String>) params.get (s);
            return list.get (0);
        }
    }

    public Enumeration getParameterNames () {
        return new ReadOnlyEnumeration (params.keySet ());
    }

    @SuppressWarnings ("unchecked")
    public String[] getParameterValues (String s) {
        Object old = params.get (s);
        if (old == null) return null;
        if (old instanceof String) return new String [] {(String) old};
        if (old instanceof List) {
            String[] a = new String[ ((List) old).size ()];
            a = ((List<String>) old).toArray (a);
            return a;
        }
        return null;
    }

    public Map getParameterMap () {
        return params;
    }

    public String getProtocol () {
        return "jasmine.initation";
    }

    public String getScheme () {
        return null;
    }

    public String getServerName () {
        return null;
    }

    public int getServerPort () {
        return 0;
    }

    public BufferedReader getReader () throws IOException {
        return null;
    }

    public String getRemoteAddr () {
        return null;
    }

    public String getRemoteHost () {
        return null;
    }

    public void setAttribute (String s, Object o) {
        attributes.put (s, o);
    }

    public void removeAttribute (String s) {
        attributes.remove (s);
    }

    public Locale getLocale () {
        return Locale.getDefault ();
    }

    public Enumeration getLocales () {
        return null;
    }

    public boolean isSecure () {
        return false;
    }

    public RequestDispatcher getRequestDispatcher (String s) {
        return context.getRequestDispatcher (s);
    }

    @Deprecated
    public String getRealPath (String s) {
        return null;
    }

    public int getRemotePort () {
        return 0;
    }

    public String getLocalName () {
        return null;
    }

    public String getLocalAddr () {
        return null;
    }

    public int getLocalPort () {
        return 0;
    }
}